package alignment_approach;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class ReadOutputAzure {

	public static boolean containsRecord(String path, String record){
		Scanner in;
		try {
			in = new Scanner(new FileReader(path));
			while (in.hasNextLine()) {
			    String line = in.nextLine();
			    if (!line.isEmpty() && line.contains(record)) {
					return true;
				}
			    
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public static int sizeAlignment(String path){
		Scanner in;
		int count = 0 ;
		try {
			in = new Scanner(new FileReader(path));
			while (in.hasNextLine()) {
			    String line = in.nextLine();
			    if (!line.isEmpty()) {
					count++;
				}
			    
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}
	
	public static void main(String[] args) {
		createUniqueFile("C:/Users/Brasileiro/Ourput Azure");
	}
	
	/** unifica todas as saidas em um unico arquivo.**/
	public static void createUniqueFile(String path){
		String lines = "";
		for (int i = 0; i < 26; i++) {
			String intput = path + "/part-r-" + calculateIndex(i);
			Scanner in;
			try {
				in = new Scanner(new FileReader(intput));
				while (in.hasNextLine()) {
				    String line = in.nextLine();
				    if (!line.isEmpty()) {
						lines += (line + "\n");
					}
				    
				}
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		try {
			escritor(path + "/Align.txt", lines);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private static String calculateIndex(int i) {
		String indexFile9 = "0000";
		String indexFile99 = "000";
		if (i < 10) {
			return indexFile9 + i;
		}
		return indexFile99 + i;
	}

	public static void escritor(String path, String linesToWrite) throws IOException {
		BufferedWriter buffWrite = new BufferedWriter(new FileWriter(path));
//		String linha = "";
//		Scanner in = new Scanner(System.in);
//		System.out.println("Escreva algo: ");
//		linha = in.nextLine();
		buffWrite.write(linesToWrite);
		buffWrite.close();
	} 


}
